//
//  LogUploadViewController.m
//  Manager
//
//  Created by 彭凯敏 on 15/9/2.
//  Copyright (c) 2015年 52xiaoluo. All rights reserved.
//

#import "LogUploadViewController.h"
#import "LogManager.h"

@interface LogUploadViewController ()
@property (weak, nonatomic) IBOutlet UILabel *startLabel;
@property (weak, nonatomic) IBOutlet UIView *startView;
@property (weak, nonatomic) IBOutlet UILabel *endLabel;
@property (weak, nonatomic) IBOutlet UIView *endView;
- (IBAction)clickTime:(UITapGestureRecognizer *)sender;


@property (nonatomic,strong)UIView *currentView;

@property (strong, nonatomic) IBOutlet UIView *pickView;
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
- (IBAction)dateChanged:(UIDatePicker *)sender;
- (IBAction)clickPickSure:(UIButton *)sender;


- (IBAction)clickYes:(UIButton *)sender;

@end

@implementation LogUploadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self baseCtlSetTitle:@"日志上报"];
    
    
}


#pragma mark - time


- (IBAction)clickTime:(UITapGestureRecognizer *)sender {
   
    _currentView = sender.view;
    
    if (_currentView == _startView) {
        
        [_datePicker setMaximumDate:[NSDate date]];
        
        [self resetDatePicker:[NSDate date]];
        
        
        _datePicker.minimumDate = nil;
        
    }
    else if (_currentView == _endView)
    {
        if ([_startLabel.text isEqualToString:@""]) {
            FailedShow(@"请先选择开始时间");
            return;
        }
        
        
        NSDate *startDate = [KMGetDate dateFromTimeStr:_startLabel.text andFormatter:@"yyyy/MM/dd HH:mm"];
        
        _datePicker.minimumDate = startDate;
        _datePicker.maximumDate = [startDate dateByAddingTimeInterval:5*60*60];
    }
    
    if (_pickView.originY == ScreenHeight-64-_pickView.sizeHeight) {
        return;
    }
    
    
    [_pickView setOriginY:ScreenHeight-64];
    [_pickView setSizeWidth:ScreenWidth];
    [_pickView setSizeHeight:IS320?225:280];
    [self.view addSubview:_pickView];
    [UIView animateWithDuration:0.3 animations:^{
        
        [_pickView setOriginY:ScreenHeight-64-_pickView.sizeHeight];
    }];
}
- (IBAction)dateChanged:(UIDatePicker *)sender {
    
    
    NSString *timeStr = [self stringFromDate:[self resetDatePicker:sender.date]];
    
    
    if (_currentView == _startView) {
        _startLabel.text = timeStr;
    }
    else if (_currentView == _endView)
    {
        _endLabel.text = timeStr;
    }
    
    
}

- (IBAction)clickPickSure:(UIButton *)sender {
    [self dateChanged:_datePicker];
    
    [UIView animateWithDuration:0.3 animations:^{
        
        [_pickView setOriginY:ScreenHeight-64];
    }];
    
}

- (IBAction)clickYes:(UIButton *)sender {
   
    
    if ([_startLabel.text isEqualToString:@""]) {
        FailedShow(@"请选择开始时间");
        return;
    }
    if ([_endLabel.text isEqualToString:@""]) {
        FailedShow(@"请选择结束时间");
        return;
    }
    NSDate *startDate = [KMGetDate dateFromTimeStr:_startLabel.text andFormatter:@"yyyy/MM/dd HH:mm"];

    NSDate *endDate = [KMGetDate dateFromTimeStr:_endLabel.text andFormatter:@"yyyy/MM/dd HH:mm"];
    
    if ([[startDate dateByAddingTimeInterval:5*60*60] timeIntervalSinceDate:endDate]<0) {
        FailedShow(@"时间间隔必须小于5小时");
        return;
    }
    
    
    
    
}

- (NSDate *)resetDatePicker:(NSDate *)date
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; //实例化一个NSCalendar对象
    NSDateComponents *comps = [[NSDateComponents alloc] init]; //实例化一个NSDateComponents对象
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | //二进制的日期元素
    NSHourCalendarUnit;
    comps = [calendar components:unitFlags fromDate:date]; //调用components方法，返回NSDateComponents对象
    
    NSDate *newDate = [calendar dateFromComponents:comps];
    [_datePicker setDate:newDate];
    
    return newDate;
}
- (NSString *)stringFromDate:(NSDate *)date{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    
    [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm"];
    
    NSString *destDateString = [dateFormatter stringFromDate:date];
    
    return destDateString;
}

@end
